/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.ui.util;

import java.awt.Container;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import org.apache.log4j.Logger;

import who.tmap.bo.cd.CDTypes;
import who.tmap.bo.cd.CartographicDisplay;
import who.tmap.bo.cd.impl.CartographicDisplayFactory;
import who.tmap.common.TMapException;
import who.tmap.dao.cd.CartographicDisplayDAO;
import who.tmap.dao.cd.impl.CDDAOFactory;

/**
 * @author stl
 *
 */
public class CartographicDisplayUIHelper extends BaseUIHelper {

    private static Logger logger = Logger.getLogger(CartographicDisplayUIHelper.class);
    
	public File saveCD( Container container, File file, CartographicDisplay cd ) {
        if ( file != null ) {
            save( container, file, cd );
            return file;
        } else {
        	return saveCDAs( container, cd );
        }		

	}    
    
	private File save( Container container, File file, CartographicDisplay cd ) {
		try {
	        CartographicDisplayDAO dao =(CartographicDisplayDAO)CDDAOFactory.getInstance().get( cd.getType() );
	        dao.save( file.getAbsolutePath(), cd );
	        cd.setFile( file );
	        
	        return file;
        } catch (TMapException e) {
            miscUIHelper.displayErrorMessage( container, e );
        }         
        
        return null;
	}
	
	public File saveCDAs( Container container, CartographicDisplay cd ) {
	    File file =null;
		JFileChooser fileChooser = new JFileChooser();
		fileChooser.setSelectedFile( new File( "*" + FileChooserFilter.CARTOGRAPHIC_DISPLAY_EXT ));
		fileChooser.addChoosableFileFilter( new FileChooserFilter( FileChooserFilter.CARTOGRAPHIC_DISPLAY_EXT ) );
		fileChooser.setCurrentDirectory( getCdDir( container ) );
		
		// Save file dialog.
		int retVal =fileChooser.showSaveDialog(container);
		
		if ( retVal == JFileChooser.APPROVE_OPTION ) {
			file =fileChooser.getSelectedFile();
		    logger.debug( "selectedFile=[" + file + "]" );
		    setCdDir( file.getParentFile() );

	        if ( file.exists() ) {

	            int n =JOptionPane.showConfirmDialog( container,
	                    "This file already exists. Do you want to erase it?",
	                    "Save Cartographic Display",
	                    JOptionPane.YES_NO_OPTION);
	            if ( n == JOptionPane.NO_OPTION ) {
	                file =null;
	            }
	        }
	        
	        if ( file != null ) {
	            save( container, file, cd );
	        }

		}
		
		return file;
	}	    
	
	public CartographicDisplay openCD( Container container )  {
		CartographicDisplay cd =null;
		JFileChooser fileChooser = new JFileChooser();
		fileChooser.addChoosableFileFilter( new FileChooserFilter( FileChooserFilter.CARTOGRAPHIC_DISPLAY_EXT ) );
		
		fileChooser.setCurrentDirectory( getCdDir( container ) );
		
		// Open file dialog.
		int retVal =fileChooser.showOpenDialog(	container );
		
		if ( retVal == JFileChooser.APPROVE_OPTION ) {		
		
			File file =fileChooser.getSelectedFile();
			setCdDir( file.getParentFile() );
		    	    
		    try {
		    	CartographicDisplayDAO dao =(CartographicDisplayDAO)CDDAOFactory.getInstance().get( file );		        
	            cd =(CartographicDisplay)dao.load( file.getAbsolutePath() );
	        } catch (TMapException e1) {
	            miscUIHelper.displayErrorMessage( container, e1 );
	        }
		    
		}
	    
	    return cd;
	}    
	
	public CartographicDisplay newCD( Container container ) {
	    CartographicDisplay cd =null;
        try {
            cd = (CartographicDisplay)CartographicDisplayFactory.getInstance().get( CDTypes.DEFAULT_CD );
        } catch (TMapException e) {
            miscUIHelper.displayErrorMessage( container, e );
        }
        
        return cd;
	}
	

    
}
